<?php


namespace app\api\controller;


use app\common\controller\Api;
use think\cache\driver\Redis;
use think\Log;

class Baidu extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
//获取百度语音accesstoken
    public function baidubcetoken(){
        $redis = new Redis();
        if($redis->has("baidubcetoken")){
            $token = $redis->get("baidubcetoken");
        }else{
            $url = 'https://aip.baidubce.com/oauth/2.0/token';
            $post_data['grant_type'] = 'client_credentials';
            /*$post_data['client_id']  = '4uKg41wk7O2Okk5PKGVlIF6G';
            $post_data['client_secret'] = 'Fq8inIEEXcy4bYBjZ9ZpGacdSwVW5HnW';*/
            $post_data['client_id']  = 'nkzZUXSol0T2YPrND7oWtK73';
            $post_data['client_secret'] = 'fTzLnKtGW4kGm9qRagfPQfEjrRIkRvHD';
            $o = "";
            foreach ( $post_data as $k => $v )
            {
                $o.= "$k=" . urlencode( $v ). "&" ;
            }
            $post_data = substr($o,0,-1);
            $res = postCurl($url.'?'.$post_data,[]);
            if($res['code']==200){
                $token = $res['data']['access_token'];
                $redis->set('baidubcetoken',$token,'2591000');
            }else{
                return [
                    'code'=>0,
                    'msg'=>'获取token失败'
                ];
            }
        }
        return [
            'code'=>1,
            'token'=>$token,
            'msg'=>'获取token成功'
        ];

    }

    //百度语音转文字
    public function baiduvoicetotext(){
        $format = $this->request->post('format');//1题目2.答
        $rate = $this->request->post('rate');
        $cuid = $this->request->post('cuid');
//        $len = $this->request->post('len');
//        $speech = $this->request->post('speech');
        $file = $_FILES['speech'];
        $file1 = request()->file('speech');
        Log::write('上传语音文件接收：--------------');
        Log::write($file);
        $info = $file1->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            Log::write('上传语音文件位置：'.ROOT_PATH . 'public' . DS . 'uploads'. DS.$info->getSaveName());
        }else{
            /*// 上传失败获取错误信息
            echo $file->getError();*/
        }
        $len = $file['size'];
        $speech = base64_encode(file_get_contents(ROOT_PATH . 'public' . DS . 'uploads'. DS.$info->getSaveName()));
        $url = 'http://vop.baidu.com/server_api';
        $tokenres  = $this->baidubcetoken();
        if($tokenres['code']==1){
            $token = $tokenres['token'];
        }else{
            $this->error('获取token失败');
        }
//        $token = '24.dafef0a43d85f8c9bf07559d73e456a7.2592000.1654335336.282335-26147631';
        $data = [
            "format"=>$format,
            "rate"=>$rate,
            "dev_pid"=>1537,
            "channel"=>1,
            "token"=>$token,
            "cuid"=>$cuid,
            "len"=>$len,
            "speech"=>$speech, // xxx为 base64（FILE_CONTENT）
        ];
        $res = postCurl($url,$data);
        if($res['code']==200){
            if($res['data']['err_no']==0){
                $this->success('获取成功',['text'=>$res['data']['result']]);
            }else{
                $this->error('失败，错误码：'.$res['data']['err_no']);
            }

        }else{
            $this->error('其他错误，联系管理员');
        }
    }

}